package com.lmz.project.redis.arithmetic.heapShed;

import java.util.Stack;

public class QueueAndStack {
    //用栈实现队列
    //用两个栈实现 in栈 out 栈
    //两个原则： 1：out空了in 才能向out倒数据，2：如果需要倒数据，in栈全部倒入out 清空in
    class MyQueue {
        public Stack<Integer> in;
        public Stack<Integer> out;

        public MyQueue() {
            in = new Stack<Integer>();
            out = new Stack<Integer>();
        }

        private void inToOut() {
            if (out.empty()) {
                while (!in.empty()) {
                    out.push(in.pop());
                }
            }
        }

        public void push(int x) {
            in.push(x);
            inToOut();
        }

        public int pop() {
            inToOut();
            return out.pop();
        }

        public int peek() {
            inToOut();
            return out.peek();
        }

        public boolean empty() {
            return in.empty() && out.empty();
        }
    }
}
